import request from '@/utils/request'

export function addTask (data, succeed, failed, completed) {
  send(request({ url: '/task/add', method: 'post', data: data }), succeed, failed, completed)
}

export function disableCluster (data, succeed, failed, completed) {
  send(request({ url: '/cluster/disable', method: 'post', data: data }), succeed, failed, completed)
}

export function disableTask (data, succeed, failed, completed) {
  send(request({ url: '/task/disable', method: 'post', data: data }), succeed, failed, completed)
}

export function editTask (data, succeed, failed, completed) {
  send(request({ url: '/task/edit', method: 'post', data: data }), succeed, failed, completed)
}

export function enableCluster (data, succeed, failed, completed) {
  send(request({ url: '/cluster/enable', method: 'post', data: data }), succeed, failed, completed)
}

export function enableTask (data, succeed, failed, completed) {
  send(request({ url: '/task/enable', method: 'post', data: data }), succeed, failed, completed)
}

export function getClusterList (parameter, succeed, failed, completed) {
  send(request({ url: '/cluster/list', method: 'get', params: parameter }), succeed, failed, completed)
}

export function getSchedulerPaging (parameter, succeed, failed, completed) {
  send(request({ url: '/message/scheduler/paging', method: 'get', params: parameter }), succeed, failed, completed)
}

export function getServletPaging (parameter, succeed, failed, completed) {
  send(request({ url: '/message/servlet/paging', method: 'get', params: parameter }), succeed, failed, completed)
}

export function getSnapshotList (parameter, succeed, failed, completed) {
  send(request({ url: '/message/snapshot/list', method: 'get', params: parameter }), succeed, failed, completed)
}

export function getTaskDetail (parameter, succeed, failed, completed) {
  send(request({ url: '/task/detail', method: 'get', params: parameter }), succeed, failed, completed)
}

export function getTaskPaging (parameter, succeed, failed, completed) {
  send(request({ url: '/task/paging', method: 'get', params: parameter }), succeed, failed, completed)
}

export function publishMessage (data, succeed, failed, completed) {
  send(request({ url: '/topic/publish', method: 'post', data: data }), succeed, failed, completed)
}

export function setCluster (data, succeed, failed, completed) {
  send(request({ url: '/cluster/edit', method: 'post', data: data }), succeed, failed, completed)
}

// Permission apis.

export function addPermissionApplication (data, succeed, failed, completed) {
  send(request({ url: '/perm/application/add', method: 'post', data: data }), succeed, failed, completed)
}

export function addPermissionResource (data, succeed, failed, completed) {
  send(request({ url: '/perm/resource/add', method: 'post', data: data }), succeed, failed, completed)
}

export function deletePermissionApplication (data, succeed, failed, completed) {
  send(request({ url: '/perm/application/delete', method: 'post', data: data }), succeed, failed, completed)
}

export function deletePermissionResource (data, succeed, failed, completed) {
  send(request({ url: '/perm/resource/delete', method: 'post', data: data }), succeed, failed, completed)
}

export function disablePermissionApplication (data, succeed, failed, completed) {
  send(request({ url: '/perm/application/disable', method: 'post', data: data }), succeed, failed, completed)
}

export function disablePermissionResource (data, succeed, failed, completed) {
  send(request({ url: '/perm/resource/disable', method: 'post', data: data }), succeed, failed, completed)
}

export function editPermissionApplication (data, succeed, failed, completed) {
  send(request({ url: '/perm/application/edit', method: 'post', data: data }), succeed, failed, completed)
}

export function editPermissionResource (data, succeed, failed, completed) {
  send(request({ url: '/perm/resource/edit', method: 'post', data: data }), succeed, failed, completed)
}

export function enablePermissionApplication (data, succeed, failed, completed) {
  send(request({ url: '/perm/application/enable', method: 'post', data: data }), succeed, failed, completed)
}

export function enablePermissionResource (data, succeed, failed, completed) {
  send(request({ url: '/perm/resource/enable', method: 'post', data: data }), succeed, failed, completed)
}

export function getPermissionAccount (parameter, succeed, failed, completed) {
  send(request({ url: '/perm/account', method: 'get', params: parameter }), succeed, failed, completed)
}

export function getPermissionApplication (parameter, succeed, failed, completed) {
  send(request({ url: '/perm/application', method: 'get', params: parameter }), succeed, failed, completed)
}

export function getPermissionResources (parameter, succeed, failed, completed) {
  send(request({ url: '/perm/resource', method: 'get', params: parameter }), succeed, failed, completed)
}

export function getPermissionResourcesNav (parameter, succeed, failed, completed) {
  send(request({ url: '/perm/resource/nav', method: 'get', params: parameter }), succeed, failed, completed)
}

// Request send and invokes.

function send (req, succeed, failed, completed) {
  req.then((res) => {
    if (typeof res !== 'undefined' && typeof res.errno !== 'undefined') {
      if (res.errno === '0' || res.errno === 0) {
        invokeSucceed(succeed, res)
        return
      }
      invokeFailed(failed, res.error)
      return
    }
    invokeFailed(failed, 'unknown response type')
  }).catch((err) => {
    invokeFailed(failed, err)
  }).finally(() => {
    invokeCompleted(completed)
  })
}

function invokeSucceed (caller, res) {
  if (typeof caller === 'function') {
    caller(res)
  }
}

function invokeFailed (caller, err) {
  if (typeof caller === 'function') {
    caller(err)
  }
}

function invokeCompleted (caller) {
  if (typeof caller === 'function') {
    caller()
  }
}
